package com.sl.biz.modular.process.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sl.auth.core.util.StpCompositeUtil;
import com.sl.core.engine.plugin.ProcessPluginLoaderManager;
import com.sl.core.engine.plugin.ProcessPluginRegistry;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/process/component")
public class ProcessComponentResourceController {


    @SneakyThrows
    @GetMapping("/version_list")
    public List<Map<String,Object>> versionList(@RequestParam("code") String code) {
        return ProcessPluginRegistry.INSTANCE.getVersionList(StpCompositeUtil.getOrgId(),code);
    }


    @SneakyThrows
    @GetMapping("/js")
    public void js(@RequestParam(value = "orgId",required = false) String orgId,@RequestParam("version") String version, @RequestParam("code") String code, HttpServletResponse serverResponse) {

        serverResponse.setContentType("application/javascript");

        //根据编码的到 js
        byte[] bytes = ProcessPluginLoaderManager.INSTANCE.getResource(StpCompositeUtil.getOrgId(),code, version, "js");
        if (ObjectUtil.isNull(bytes)) {
            return;
        }
        serverResponse.getOutputStream().write(bytes);
    }

    @SneakyThrows
    @GetMapping("/css")
    public void css(@RequestParam(value = "orgId",required = false) String orgId,@RequestParam("version") String version, @RequestParam("code") String code, HttpServletResponse serverResponse) {

        serverResponse.setContentType("text/css");

        //根据编码的到 js
        byte[] bytes = ProcessPluginLoaderManager.INSTANCE.getResource(StpCompositeUtil.getOrgId(),code, version, "css");
        if (ObjectUtil.isNull(bytes)) {
            return;
        }
        serverResponse.getOutputStream().write(bytes);
    }

    @SneakyThrows
    @GetMapping("/html")
    public void html(@RequestParam(value = "orgId",required = false) String orgId,@RequestParam("version") String version, @RequestParam("code") String code, HttpServletResponse serverResponse) {
        serverResponse.setContentType("text/html");
        //根据编码的到 js
        byte[] bytes = ProcessPluginLoaderManager.INSTANCE.getResource(StpCompositeUtil.getOrgId(),code, version, "html");
        if (ObjectUtil.isNull(bytes)) {
            return;
        }
        serverResponse.getOutputStream().write(bytes);
    }
}
